package com.zat.mes.andrmes.dofun.work

import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.google.gson.Gson
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseActivity
import com.zat.mes.andrmes.customerview.HeadDateView
import com.zat.mes.andrmes.data.LoginResultBean
import com.zat.mes.andrmes.dofun.work.fragment.DayPplanClassListFragment
import com.zat.mes.andrmes.dofun.work.fragment.DayProducePlanAndMaterialUsedFragment
import com.zat.mes.andrmes.dofun.work.fragment.DayProducePlanFragment
import com.zat.mes.andrmes.dofun.work.vm.WorkViewModel
import com.zat.mes.andrmes.event.WorkDayPlanEventMessage
import com.zat.mes.andrmes.global.CompanionType
import com.zat.mes.andrmes.utils.DateUtil
import com.zat.mes.andrmes.utils.GlideUtil
import com.zat.mes.andrmes.utils.PreferencesUtil
import kotlinx.android.synthetic.main.layout_head_clock.*
import kotlinx.android.synthetic.main.layout_title.*
import org.greenrobot.eventbus.EventBus
import java.util.*

/**
 * 做工计划activity
 */
class DayProducePlanActivity : BaseActivity() {

    var selectedDate = DateUtil.formatDate(Date(), DateUtil.FORMAT_3)
    var empId: Long = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun init() {
        initView()
        iniData()
        initModel()
    }

    /**
     * 初始化viewmodel
     */
    private fun initModel() {
        val workViewModel: WorkViewModel = ViewModelProvider(this).get(WorkViewModel::class.java)
        workViewModel.getAuthList().observe(this, androidx.lifecycle.Observer {
            if (it == null || it?.size <= 0) {
                //直接员工的日计划列表
                val beginTransaction = supportFragmentManager.beginTransaction()
                beginTransaction.add(R.id.flContentAllDp, DayProducePlanFragment.newInstance(empId))
                    .commit()
            } else {
                //fragment 班组
                //请求班组列表
                var ids: StringBuilder = StringBuilder()
                for (i in 0..it.size - 1) {
                    ids.append(it.get(i))
                    if (i != it.size - 1) {
                        ids.append(",")
                    }
                }
                val beginTransaction = supportFragmentManager.beginTransaction()
                beginTransaction.add(
                    R.id.flContentAllDp,
                    DayPplanClassListFragment.newInstance(empId, ids.toString())
                )
                    .commit()

            }
        })
        empId?.let { workViewModel.loadAuthList(it) }

    }

    /**
     * 初始化数据
     */
    private fun iniData() {

        val loginResultBean = Gson().fromJson(
            PreferencesUtil.getString(CompanionType.LOGIN_RESULT),
            LoginResultBean::class.java
        )
        empId = loginResultBean.empid
        headDateView.setUserName(loginResultBean.empname)
        headDateView.setTimeName(getString(R.string.text_plan_query))
        GlideUtil.setHeader(this, loginResultBean?.img, ivHeadClock)
        val timeListener = object : HeadDateView.OnHeadTimeSelectListener {
            override fun onTimeSelect(date: String) {
                selectedDate = date
                //进行接口的刷新 发送事件
                EventBus.getDefault().postSticky(WorkDayPlanEventMessage(selectedDate))
            }

        }
        headDateView.headTimeSelectListener = timeListener


    }

    /**
     * 初始化view
     */
    private fun initView() {
        setContentView(R.layout.activity_day_produce_plan)
        tVtitle.text = getString(R.string.title_produce_plan)
        rlReturnTitle.visibility = View.VISIBLE
        rlRight.visibility = View.INVISIBLE
        rlReturnTitle.setOnClickListener { finish() }
    }


}
